﻿using System;
using System.Collections.Generic;
using System.Web;
using OneMG.Phoenix.Service.Model;
using OneMG.Phoenix.Service.Contracts;
using OneMG.Phoenix.Service;
using OneMG.Phoenix.Kernel.Util;
using OneMG.Phoenix.Kernel.Enum;

namespace OneMG.Phoenix.AdminSite.Views.AdminUser
{

    public class Save : IHttpHandler
    {
        IAdminUserService IAdminUser = new AdminUserService();

        public void ProcessRequest(HttpContext context)
        {
            int actionType = 0;
            int.TryParse(context.Request.Form["hdaction"], out actionType);
            string action = "";

            switch ((ActionType)actionType)
            {
                case ActionType.Insert:
                    {
                       Add(context);
                       action = "添加成功";
                    }
                    break;
                case ActionType.Delete:
                    {
                        delete(context);
                        action = "删除成功";
                    }
                    break;
                case ActionType.Modify:
                    {
                        update(context);
                        action = "修改成功";
                    }
                    break;

            }

            JsonResult.Result(context, JsonMessageStatus.OK, action);

        }

        private void Add(HttpContext context)
        {
            AdminUserModel model = new AdminUserModel()
            {
                UserName = context.Request.Form["txtUserName"],
                Password = context.Request.Form["txtPassword"],
                TrueName = context.Request.Form["txtTrueName"],
                Telephone = context.Request.Form["txtTelephone"],
                EMail = context.Request.Form["txtEMail"],
                Memo = context.Request.Form["txtMemo"],
                CreateTime = DateTime.Now,
                CreateUser = 1,
                UpdateTime = DateTime.Parse("1900-01-01"),
                UpdateUser = 1,
                Status = 1,
                DataPermisType = 1,
                DeleteFlag = false,
                DeleteTime = DateTime.Parse("1900-01-01"),
                DeleteUser = 0,
                LastLoginTime = DateTime.Parse("1900-01-01"),
                LockTime = DateTime.Parse("1900-01-01"),
                OrgID = 0,
                OrgIDPath = "",
                OrgNamePath = "",
                Visitation = 0,
            };

            int userID= IAdminUser.AddAdminUser(model);

        }

        private void update(HttpContext context)
        {
            int userID = 0;
            int.TryParse(context.Request.Form["hduserid"], out userID);
            AdminUserModel model = IAdminUser.GetByUserID(userID);
            model.Password = context.Request.Form["txtPassword"];
            model.TrueName = context.Request.Form["txtTrueName"];
            model.Telephone = context.Request.Form["txtTelephone"];
            model.EMail = context.Request.Form["txtEMail"];
            model.Memo = context.Request.Form["txtMemo"];
            model.UpdateUser = 1;
            model.UpdateTime = DateTime.Now;
            IAdminUser.UpdateByUserID(model);
        }

        private void delete(HttpContext context)
        {
            int userID = 0;
            int.TryParse(context.Request.Form["hduserid"], out userID);
            IAdminUser.DeleteByUserID(userID);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
